import { create } from 'zustand';

interface EditorState {
  isEditing: boolean;
  isDirty: boolean;
  originalContent: string;
  currentContent: string;
  language: string;
  theme: 'light' | 'dark';
  fontSize: number;
  
  startEditing: (content: string, language: string) => void;
  updateContent: (content: string) => void;
  discardChanges: () => void;
  setLanguage: (language: string) => void;
  setTheme: (theme: 'light' | 'dark') => void;
  setFontSize: (size: number) => void;
  resetEditor: () => void;
}

export const useEditorStore = create<EditorState>((set, get) => ({
  isEditing: false,
  isDirty: false,
  originalContent: '',
  currentContent: '',
  language: 'plaintext',
  theme: 'light',
  fontSize: 14,

  startEditing: (content: string, language: string) => {
    set({
      isEditing: true,
      originalContent: content,
      currentContent: content,
      language,
      isDirty: false,
    });
  },

  updateContent: (content: string) => {
    const { originalContent } = get();
    set({
      currentContent: content,
      isDirty: content !== originalContent,
    });
  },

  discardChanges: () => {
    const { originalContent } = get();
    set({
      currentContent: originalContent,
      isDirty: false,
    });
  },

  setLanguage: (language: string) => {
    set({ language });
  },

  setTheme: (theme: 'light' | 'dark') => {
    set({ theme });
  },

  setFontSize: (size: number) => {
    set({ fontSize: size });
  },

  resetEditor: () => {
    set({
      isEditing: false,
      isDirty: false,
      originalContent: '',
      currentContent: '',
      language: 'plaintext',
    });
  },
}));